import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      name: 'Home',
      component: () => import('../components/sections/HomeSection.vue')
    },
    {
      path: '/map',
      name: 'Map',
      component: () => import('../components/map/MapSection.vue')
    },
    {
      path: '/attractions',
      name: 'Attractions',
      component: () => import('../components/sections/Attractions.vue')
    },
    {
      path: '/planner',
      name: 'TripPlanner',
      component: () => import('../components/sections/TripPlanner.vue')
    },
    {
      path: '/admin',
      name: 'AdminPanel',
      component: () => import('../components/sections/AdminPanel.vue')
    },
    {
      path: '/attraction/:id',
      name: 'AttractionDetail',
      component: () => import('../components/views/AttractionDetail.vue')
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('../views/LoginView.vue')
    },
    {
      path: '/register',
      name: 'Register',
      component: () => import('../views/RegisterView.vue')
    },
    {
      path: '/profile',
      name: 'Profile',
      component: () => import('../views/ProfileView.vue'),
      meta: { requiresAuth: true }
    }
  ]
})

// 全局导航守卫
router.beforeEach((to, from, next) => {
  const userStore = JSON.parse(localStorage.getItem('userInfo') || 'null')
  
  // 如果页面需要认证且用户未登录
  if (to.meta.requiresAuth && !userStore) {
    next('/login')
  } else {
    next()
  }
})

export default router